﻿/*!
 * Knockout+ Library v1.1.0
 * https://knockoutplus.codeplex.com/
 *
 * Copyright 2014 Janusz Noszczyński and other contributors
 * Released under the MIT license
 * https://knockoutplus.codeplex.com/license
 *
 * More documentation and examples
 * http://blog.coderiseventures.com/
 */

(function ($, ko)
{
    function parseBool(val)
    {
        if (val === "true")
        {
            return true;
        }

        return false;
    }

    ko.bindingHandlers.icon =
    {
        init: function (element, valueAccessor)
        {
            var accessor = valueAccessor();
            var val = ko.utils.unwrapObservable(accessor);
            var $element = $(element);

            $element.addClass("glyphicon").addClass("glyphicon-" + val);
        }
    };

    ko.bindingHandlers.modal =
    {
        init: function(element, valueAccessor, allBindings, viewModel, bindingContext)
        {
            var $element = $(element);
            var accessor = valueAccessor();
            var val = ko.utils.unwrapObservable(accessor);

            var showAttr = $element.attr("data-show");
            var show = (typeof showAttr !== 'undefined') ? parseBool(showAttr) : true;

            var keyboardAttr = $element.attr("data-keyboard");
            var keyboard = (typeof keyboardAttr !== 'undefined') ? parseBool(keyboardAttr) : true;

            var remoteAttr = $element.attr("data-remote");
            var remote = (typeof remoteAttr !== 'undefined') ? remoteAttr : false;

            var backdropAttr = $element.attr("data-backdrop");
            var backdrop = (typeof backdropAttr !== 'undefined') ? parseBool(remoteAttr) : true;

            var options = {};
            options.show = typeof val.show !== 'undefined' ? ko.utils.unwrapObservable(val.show) : show;
            options.keyboard = typeof val.keyboard !== 'undefined' ? ko.utils.unwrapObservable(val.keyboard) : keyboard;
            options.remote = typeof val.remote !== 'undefined' ? ko.utils.unwrapObservable(val.remote) : remote;
            options.backdrop = typeof val.backdrop !== 'undefined' ? ko.utils.unwrapObservable(val.backdrop) : backdrop;

            var modal = $element.modal(options);

            val.show.subscribe(function (newValue)
            {
                if (ko.utils.unwrapObservable(newValue))
                {
                    $element.modal('show');
                }
                else
                {
                    $element.modal('hide');
                }
            });

            $element.on('shown.bs.modal', function (e)
            {
                val.show(true);
            });

            $element.on('hidden.bs.modal', function (e)
            {
                val.show(false);
            });
        }
    };

    ko.bindingHandlers.popover =
    {
        init: function (element, valueAccessor, allBindings, viewModel, bindingContext)
        {
            var $element = $(element);
            var accessor = valueAccessor();
            var val = ko.utils.unwrapObservable(accessor);

            var animationAttr = $element.attr("data-animation");
            var animation = (typeof animationAttr !== 'undefined') ? parseBool(animationAttr) : true;

            var htmlAttr = $element.attr("data-html");
            var html = (typeof htmlAttr !== 'undefined') ? parseBool(htmlAttr) : false;

            var placementAttr = $element.attr("data-placement");
            var placement = (typeof placementAttr !== 'undefined') ? placementAttr : "right";

            var selectorAttr = $element.attr("data-selector");
            var selector = (typeof selectorAttr !== 'undefined') ? parseBool(selectorAttr) : false;

            var triggerAttr = $element.attr("data-trigger");
            var trigger = (typeof triggerAttr !== 'undefined') ? parseBool(triggerAttr) : "click";

            var titleAttr = $element.attr("data-title");
            var title = (typeof titleAttr !== 'undefined') ? parseBool(titleAttr) : "";

            var contentAttr = $element.attr("data-content");
            var content = (typeof contentAttr !== 'undefined') ? parseBool(contentAttr) : true;

            var delayAttr = $element.attr("data-delay");
            var delay = (typeof delayAttr !== 'undefined') ? parseBool(delayAttr) : 0;

            var containerAttr = $element.attr("data-container");
            var container = (typeof containerAttr !== 'undefined') ? parseBool(containerAttr) : false;

            var options = {};
            options.animation = typeof val.animation !== 'undefined' ? ko.utils.unwrapObservable(val.animation) : animation;
            options.html = typeof val.html !== 'undefined' ? ko.utils.unwrapObservable(val.html) : html;
            options.placement = typeof val.placement !== 'undefined' ? ko.utils.unwrapObservable(val.placement) : placement;
            options.selector = typeof val.selector !== 'undefined' ? ko.utils.unwrapObservable(val.selector) : selector;
            options.trigger = typeof val.trigger !== 'undefined' ? ko.utils.unwrapObservable(val.trigger) : trigger;
            options.title = typeof val.title !== 'undefined' ? ko.utils.unwrapObservable(val.title) : title;
            options.content = typeof val.content !== 'undefined' ? ko.utils.unwrapObservable(val.content) : content;
            options.delay = typeof val.delay !== 'undefined' ? ko.utils.unwrapObservable(val.delay) : delay;
            options.container = typeof val.container !== 'undefined' ? ko.utils.unwrapObservable(val.container) : container;

            var popover = $element.popover(options);
            
            val.show.subscribe(function (newValue)
            {
                if (ko.utils.unwrapObservable(newValue))
                {
                    $element.popover('show');
                }
                else
                {
                    $element.popover('hide');
                }
            });

            $element.on('shown.bs.modal', function (e)
            {
                val.show(true);
            });

            $element.on('hidden.bs.modal', function (e)
            {
                val.show(false);
            });
        }
    };
})
(jQuery, ko);